package com.hnx.admin.system.service.impl;

import com.hnx.admin.api.service.cache.CacheAPI;
import com.hnx.admin.redis.RedisCache;
import com.hnx.admin.system.domain.dto.conf.SysConfigDTO;
import com.hnx.admin.system.domain.param.conf.SysConfigParam;
import com.hnx.admin.system.mapper.conf.ConfigMapper;
import com.hnx.admin.system.service.SysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/*
项目名称： core
创 建 人: 倔强的头发
创建日期： 2025/5/29   11:06:38 
描   述：
*/
@Service
@RequiredArgsConstructor
public class SysConfigServiceImpl implements SysConfigService, CacheAPI {
    private final RedisCache redisCache;
    private final ConfigMapper configMapper;
    @Override
    public boolean selectCaptchaEnabled() {
        String captchaEnabled = selectConfigByKey("sys.account.captchaEnabled");
        if (!StringUtils.hasText(captchaEnabled))
        {
            return true;
        }
        return Boolean.parseBoolean(captchaEnabled);
    }

    @Override
    public String selectConfigByKey(String configKey) {
        String configValue = redisCache.getCacheObject(configKey, String.class);
        if (StringUtils.hasText(configValue))
        {
            return configValue;
        }
        SysConfigParam config = new SysConfigParam();
        config.setConfigKey(configKey);
        SysConfigDTO retConfig = configMapper.selectConfig(config);
        if (Objects.nonNull(retConfig))
        {
            redisCache.setCacheObject(configKey(configKey), retConfig.getConfigValue());
            return retConfig.getConfigValue();
        }
        return "";
    }
}
